package com.ssm.service.implement;

import com.ssm.dao.GoodMapper;
import com.ssm.pojo.Good;
import com.ssm.service.GoodService;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: exercise1_online_shopping
 * @author: qin haojun
 * @create: 2021-02-14 16:48
 */
public class GoodServiceImpl implements GoodService {

    @Autowired
    GoodMapper goodMapper;

    /**
     * 通过货物名（唯一）更改货物数量
     * @param name
     * @param total
     * @throws Exception
     */
    @Override
    public void updateTotalByName(String name, Integer total) throws Exception {
        Good good=new Good();
        good.setGname(name);
        good.setTotal(total);
        goodMapper.updateTotalByName(good);
    }

    /**
     * 通过货物id（唯一）更改货物数量
     * @param goodId
     * @param total
     * @throws Exception
     */
    @Override
    public void updateTotalById(Integer goodId, Integer total) throws Exception {
        Good good=new Good();
        good.setGid(goodId);
        good.setTotal(total);
        goodMapper.updateTotalByName(good);
    }

    @Override
    public Good selectGoodByName(String name) throws Exception {
        return goodMapper.selectGoodByName(name);
    }

    @Override
    public Good selectGoodById(Integer id) throws Exception {
        return goodMapper.selectGoodById(id);
    }

    @Override
    public List<Good> selectGoodsByKeyWord(String keyWord) throws Exception {
        return goodMapper.selectGoodsByKeyWord(keyWord);
    }

    @Override
    public List<Good> getGoodList() throws Exception {
        return goodMapper.getGoodList();
    }

    @Override
    public Integer getSellCnt(Integer gid) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("gid",gid);
        goodMapper.getSellCnt(map);
        Integer cnt=(Integer) map.get("cnt");
        return cnt;
    }
}
